

import 'dart:async';

import 'package:dolqun_movies/Entities/RequestPaymentResponse.dart';
import 'package:dolqun_movies/Entities/UserInformationResult.dart';
import 'package:dolqun_movies/StateManagement/ModelProvider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wechat_kit/wechat_kit.dart';

class WechatApiModel extends Model {
  Wechat wechatApi;
  String token;
  Stream<UserInformationResult> onUserSignIn;

  UserInformationResult userInfo;
  WechatApiModel({this.wechatApi});
  Future<void> setToken(String token) async {
    var sharedPreference = await SharedPreferences.getInstance();
    await sharedPreference.setString("token", token);
  }
  Future<String> getToken() async {
    var sharedPreference = await SharedPreferences.getInstance();
    return sharedPreference.getString("token");
  }

  Future<void> signInWithWechat({String state}) {
    return wechatApi.auth(scope: [WechatScope.SNSAPI_USERINFO],state: state);
  }

  Future<bool> payment(ResultOfPaymentRequest information) {
    Completer<bool> completer = Completer();
    StreamSubscription<WechatPayResp> subscription;
    subscription = this.wechatApi.payResp().listen((event) {
      subscription.cancel();
      completer.complete(event.isSuccessful());
    });
    return this.wechatApi.pay(appId: information.appId, partnerId: information.partnerId, prepayId: information.prepayId, package: information.package, nonceStr: information.nonceStr, timeStamp: information.timeStamp, sign: information.sign)
    .then((value) => completer.future);
  }
}
